package com.hydra.wiki.web.task;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


@Log4j
@Component
public class SysInit extends HttpServlet implements CommandLineRunner {

    private static final long serialVersionUID = 1L;
    @Autowired
    private TaskListInter taskListInter;
    @Autowired
    private ServletContext servletContext;

    // 配置系统所有默认启动任务

    public SysInit() {
        super();
    }

    public void destroy() {
        super.destroy();
    }

    public void init() throws ServletException {
        /**
         * 任务集合
         */
        List<ServletInitJobInter> list = taskListInter.getTasks();
        log.info("--系统准备运行" + list.size() + "项");
        int n = 0;
        for (ServletInitJobInter servletInitJobInter : list) {
            n++;
            try {
                servletInitJobInter.execute(servletContext);
            } catch (Exception e) {
                log.error("第" + n + "项任务启动失败：" + e.getMessage());
            }
        }
    }

    @Override
    public void run(String... args) throws Exception {
        init();
    }
}
